﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SapB1WebApp.BLL;
using System.Data;
using System.Web.Services;
using System.Web.Script.Services;
using SapB1WebApp.DAL;
using SapB1WebApp.Utils;
using Newtonsoft.Json;

namespace SapB1WebApp
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                string passWord = "sapb1addon.com.@123456789()";
                string expiredDate = System.Configuration.ConfigurationManager.AppSettings["ExpiredDate"].ToString();
                expiredDate = StringCipher.Decrypt(expiredDate, passWord);
                DateTime exDate = Utils.Utils.ConvertToDate(expiredDate);

                if (DateTime.Now.Date > exDate)
                {
                    Response.Redirect("AccessDenied.htm");
                }
            }
            catch (Exception ex)
            {
                Log.WriteException(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "SystemError", ex);
                Response.Redirect("AccessDenied.htm");
            }
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
        public static string GetLogin(string userName, string passWord, string companyDB, string companyName)
        {
            try
            {
                LoginBLL pmt = new LoginBLL();
                string resultMess = pmt.GetLogin(userName, passWord, companyDB);
                if (resultMess == "SUCCESS")
                {
                    HttpContext.Current.Session["LoginUserName"] = userName;
                    HttpContext.Current.Session["LoginPassword"] = passWord;
                    HttpContext.Current.Session["CompanyDB"] = companyDB;
                    HttpContext.Current.Session["CompanyName"] = companyName;
                }
                return resultMess;
            }
            catch (Exception ex)
            {
                Log.WriteException(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "SystemError", ex);
            }
            return string.Empty;
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public static string GetAllCompany()
        {

            CommonBLL obj = new CommonBLL();
            DataSet dsData = obj.GetAllCompany("sbo-common");
            if (dsData != null && dsData.Tables.Count > 0)
            {
                return JsonConvert.SerializeObject(dsData.Tables[0]);
            }
            return string.Empty;
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
        public static void Logout()
        {
            HttpContext.Current.Session.Clear();
            HttpContext.Current.Session.Abandon();
        }
    }
}